package com.bsy.network.adapter

import com.google.gson.TypeAdapter
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter

class StringNullAdapter : TypeAdapter<String>() {
    override fun write(writer: JsonWriter?, value: String?) {
        if (value == null) {
            writer?.nullValue()
            return
        }
        writer?.value(value);
    }

    override fun read(reader: JsonReader?): String {
        // 原先是返回Null，这里改为返回空字符串
        if (reader?.peek() === JsonToken.NULL) {
            reader.nextNull()
            return ""
        }

        reader?.nextString()?.let {
            return if (it == "null") {
                ""
            } else {
                it
            }
        }
        return ""
    }
}